package com.dscs.dians;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.EditText;
import android.widget.LinearLayout;

public class MainActivity extends AppCompatActivity {
    WebView webView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getWindow().getDecorView().setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_LOW_PROFILE
                        | View.SYSTEM_UI_FLAG_FULLSCREEN
                        | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                        | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                        | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                        | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_IMMERSIVE
        );

        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_main);
        webView = findViewById(R.id.webView);
        String url = "https://m.baidu.com/";

        final SharedPreferences sharedPref = this.getSharedPreferences(
                "url", Context.MODE_PRIVATE);
        url = sharedPref.getString("url", "https://m.baidu.com/");
        final EditText editText = new EditText(MainActivity.this);
        editText.setText(url);
        LinearLayout linearLayout = new LinearLayout(this);
        linearLayout.addView(editText);
        LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) editText.getLayoutParams();
        lp.leftMargin = 10;
        linearLayout.setLayoutParams(lp);
        AlertDialog.Builder inputDialog =
                new AlertDialog.Builder(MainActivity.this);
        inputDialog.setTitle("请输入url").setView(linearLayout);
        inputDialog.setCancelable(false);
        inputDialog.setPositiveButton("确定",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        SharedPreferences.Editor editor = sharedPref.edit();
                        editor.putString("url", editText.getText().toString().trim());
                        editor.apply();
                        webView.loadUrl(editText.getText().toString().trim());
                        //系统默认会通过手机浏览器打开网页，为了能够直接通过WebView显示网页，则必须设置
                        webView.setWebViewClient(new WebViewClient(){
                            @Override
                            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                                //使用WebView加载显示url
                                view.loadUrl(url);
                                //返回true
                                return true;
                            }
                        });
                    }
                }).show();


    }
}